//
//  LRPictureView.swift
//  LRWeibo
//
//  Created by admin on 15/9/26.
//  Copyright © 2015年 懒人. All rights reserved.
//

import UIKit

private let pictureItemWidth: CGFloat = 90
private let pictureItemMargin: CGFloat = 10
// 列数
private let columnCount = 3
class LRPictureView: UICollectionView {

    var viewModel: LRStatusListModel?{
    
        didSet{
           sizeToFit()
        }
    }

    override func sizeThatFits(size: CGSize) -> CGSize {

        return calcViewSize()
    }
     // MARK: - 根据图片数量计算大小
    private func calcViewSize()->CGSize
    {
        let pictureCount = viewModel?.pic_urls?.count

        // 0张图片
        if pictureCount == 0
        {
            return CGSizeZero
        }
        
        // 1张
        if pictureCount == 1
        {
            return CGSizeMake(150, 150)
        }
        
        // 2、4张
        if pictureCount == 2 || pictureCount == 4
        {
            let w = pictureItemWidth * 2 + pictureItemMargin
            let h = CGFloat(pictureCount! / 2) * (pictureItemWidth + pictureItemMargin)
            return CGSizeMake(w, h)
        }
        
        // 其他的
        let row = (pictureCount! - 1) / columnCount + 1
        let w = pictureItemWidth * CGFloat(columnCount) + pictureItemMargin * CGFloat((columnCount - 1))
        let h = pictureItemWidth * CGFloat(row) + pictureItemMargin * CGFloat((row - 1))
        return CGSize(width: w, height: h)
    }
    
    init()
    {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSizeMake(pictureItemWidth, pictureItemWidth)
        layout.minimumLineSpacing = pictureItemMargin
        layout.minimumInteritemSpacing = pictureItemMargin
        
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        backgroundColor = UIColor.lightGrayColor()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
